package at.fhooe.engine.input;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import at.fhooe.engine.event.events.KeyDownEvent;
import at.fhooe.engine.event.events.KeyUpEvent;
import at.fhooe.engine.event.events.MouseButtonDownEvent;
import at.fhooe.engine.event.events.MouseButtonUpEvent;
import at.fhooe.engine.event.events.MouseMovedEvent;
import at.fhooe.engine.pandora.Game;

public class InputManager implements KeyListener, MouseListener, MouseMotionListener {
	
	public InputManager() {}
	
	private volatile boolean[] keyCodes = new boolean[200];
	
	public void keyPressed(KeyEvent key) {
		this.keyCodes[key.getKeyCode()] = true;

		KeyDownEvent keyEvent = new KeyDownEvent(key.getKeyCode());
		Game.getInstance().getEventManager().dispatchEvent(keyEvent);
	}

	@Override
	public void keyReleased(KeyEvent key) {
		this.keyCodes[key.getKeyCode()] = false;
		
		KeyUpEvent keyEvent = new KeyUpEvent(key.getKeyCode());
		Game.getInstance().getEventManager().dispatchEvent(keyEvent);
	}

	@Override
	public void keyTyped(KeyEvent key) {
		//Currently not supported in this version
	}

	public boolean getCurrentKeyState(int keycode) {
		return this.keyCodes[keycode];
	}

	@Override
	public void mouseClicked(MouseEvent e) {
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		MouseButtonDownEvent mouseEvent = new MouseButtonDownEvent(e.getButton(), e.getPoint().x, e.getPoint().y);
		Game.getInstance().getEventManager().dispatchEvent(mouseEvent);
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		MouseButtonUpEvent mouseEvent = new MouseButtonUpEvent(e.getButton(), e.getPoint().x, e.getPoint().y);
		Game.getInstance().getEventManager().dispatchEvent(mouseEvent);
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		MouseMovedEvent mouseEvent = new MouseMovedEvent(e.getX(), e.getY());
		Game.getInstance().getEventManager().dispatchEvent(mouseEvent);
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		MouseMovedEvent mouseEvent = new MouseMovedEvent(e.getX(), e.getY());
		Game.getInstance().getEventManager().dispatchEvent(mouseEvent);
	}

}
